using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP.Classes;

namespace ERP.View
{
    public partial class viewListChucdanh : DevExpress.XtraEditors.XtraForm
    {
        public viewListChucdanh()
        {
            InitializeComponent();
        }

        public cs_hmr_chucdanhInfo chucdanh = new cs_hmr_chucdanhInfo();
        //public cs_hmr_donviInfo dv = new cs_hmr_donviInfo();
        //public cs_hmr_nhanvienInfo nv = new cs_hmr_nhanvienInfo();
        //public cs_hmr_nv_pbInfo nvpb = new cs_hmr_nv_pbInfo();
        public int flag = 0;
        public int count = 0;
        public cs_hmr_usersInfo user = new cs_hmr_usersInfo();


        private void btnMoi_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            this.flag = 1;
            //this.nvpb.trangthai = 0;
            //this.cboDonvi.Focus();
            this.btnLuu.Enabled = true;
            this.btnThoat.Enabled = true;
        }

        private void btnDieuchinh_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            this.flag = 2;

            if (this.count > 0 && this.gridView1.FocusedRowHandle >= 0 && this.gridView1.FocusedRowHandle <= this.count)
            {
                this.chucdanh.machucdanh = this.gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, grdcID).ToString().Trim();
                if (this.chucdanh.Get_Info() > 0)
                {
                    //this.cboDonvi.Focus();
                    this.btnLuu.Enabled = true;
                    this.btnThoat.Enabled = true;
                    //this.Load_Info();
                    this.txtTennhom.Text = this.chucdanh.tenchucdanh;
                    this.txtGhichu.Text = this.chucdanh.ghichu;
                    this.txtTennhom.Focus();
                }
            }
        }

        private void btnXoa_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            if (this.count > 0 && this.gridView1.FocusedRowHandle >= 0 && this.gridView1.FocusedRowHandle <= this.count)
            {
                this.chucdanh.machucdanh = this.gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, grdcID).ToString().Trim();
                if (this.chucdanh.Get_Info() > 0)
                {
                    this.chucdanh.ExcuteProcedure(3);
                    DataSet ds = new DataSet("ds");
                    ds = this.chucdanh.Get_List();
                    if (ds == null || ds.Tables[0].Rows.Count == 0)
                    {
                        this.count = 0;
                    }
                    else
                    {
                        this.count = ds.Tables[0].Rows.Count;
                        this.grcDanhsach.DataSource = ds.Tables[0];
                    }
                }
            }
        }

        private void btnExit_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            this.Dispose(true);
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.btnThoat.Enabled = false;
            this.btnLuu.Enabled = false;
            this.flag = 0;
        }

        private void Save_Info()
        {
            this.chucdanh.tenchucdanh = this.txtTennhom.Text.Trim();
            this.chucdanh.ghichu = this.txtGhichu.Text.Trim();
            if (this.flag == 1)
            {
                this.chucdanh.ngaynhap = DateTime.Now.ToShortDateString();
                this.chucdanh.nguoinhap = this.user.user_id;
                this.chucdanh.ngaysua = DateTime.Now.ToShortDateString();
                this.chucdanh.nguoisua = this.user.user_id;
            }
            else
            {
                this.chucdanh.ngaysua = DateTime.Now.ToShortDateString();
                this.chucdanh.nguoisua = this.user.user_id;
            }
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            this.Save_Info();
            if (this.chucdanh.tenchucdanh == "")
            {

            }
            else
            {
                this.chucdanh.ExcuteProcedure(this.flag);
                DataSet ds = new DataSet("ds");
                ds = this.chucdanh.Get_List();
                if (ds == null || ds.Tables[0].Rows.Count == 0)
                {
                    this.count = 0;
                }
                else
                {
                    this.count = ds.Tables[0].Rows.Count;
                    this.grcDanhsach.DataSource = ds.Tables[0];
                }
            }
        }

        private void viewListChucdanh_Load(object sender, EventArgs e)
        {
            DataSet ds = new DataSet("ds");
            ds = this.chucdanh.Get_List();
            if (ds == null || ds.Tables[0].Rows.Count == 0)
            {
                this.count = 0;
            }
            else
            {
                this.count = ds.Tables[0].Rows.Count;
                this.grcDanhsach.DataSource = ds.Tables[0];
            }
            this.CheckUser();
        }

        private void CheckUser()
        {
            cs_hmr_userrightInfo userright = new cs_hmr_userrightInfo();
            userright.groupid = this.user.groupid;
            cs_hmr_winformInfo wf = new cs_hmr_winformInfo();
            wf.tenform = this.Name;
            if (wf.Get_Info() > 0)
            {
                userright.form_id = wf.formid;
                userright.Get_Info();
                if (userright.savedata == 0)
                {
                    this.btnMoi.Enabled = false;
                }
                if (userright.deletedata == 0)
                {
                    this.btnXoa.Enabled = false;
                }
                if (userright.updatedata == 0)
                {
                    this.btnDieuchinh.Enabled = false;
                }
                if (userright.printreport == 0)
                {
                }
            }
        }
    }
}